/**
 * 
 */
package com.cn.practice.factory;

/**
 * 工厂模式-抽象工厂
 * 
 * @author zhangyang
 *
 */
public class AbsFactoryDemo {

	public static void main(String[] args) {
		MessageSender sender = new MessageSender(new TXMessageFactory(), "BJ");
		sender.sendMessage();
	}
}

interface AbsMessageFactory {
	// 发送短信模板
	Message sendMessage(String messageType);
}

/**
 * 腾讯工厂实现了工厂
 * 
 * @author zhangyangO
 *
 */
class TXMessageFactory implements AbsMessageFactory {

	@Override
	public Message sendMessage(String messageType) {
		Message message = null;
		if (messageType.equals("BJ")) {
			message = new BJTXMessage();
		} else if (messageType.equals("SH")) {
			message = new SHTXMessage();
		}
		return message;
	}

}

class QNMessageFactory implements AbsMessageFactory {

	@Override
	public Message sendMessage(String messageType) {
		Message message = null;
		if (messageType.equals("BJ")) {
			message = new BJTQNMessage();
		} else if (messageType.equals("SH")) {
			message = new SHTQNMessage();
		}
		return message;
	}

}

/**
 * 短信发送类
 * 
 * @author zhangyang
 *
 */
class MessageSender {
	AbsMessageFactory factory;
	String messageType;

	public MessageSender(AbsMessageFactory factory, String messageType) {
		this.factory = factory;
		this.messageType = messageType;
	}

	void sendMessage() {
		Message sendMessage = this.factory.sendMessage(messageType);
		if (sendMessage == null) {
			System.out.println("当前工厂没有生成信息");
			return;
		}
		sendMessage.send();
		sendMessage.getMessage();
		sendMessage.callback();
	}

}